﻿using ResourceSystem;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using UnityEditor;
using UnityEngine;
public class UIEditorWindow:EditorWindow
{
    [MenuItem("CustomTools/UI/UIEditorWindow")]
    static void OpenUIEditorWindow()
    {
        UIEditorWindow window= GetWindow<UIEditorWindow>();
        WindowPath = PlayerPrefs.GetString("WindowPath");
        window.relativePath = PathUtils.GetRelativePath("Assets", WindowPath);
        CodeOutPath = Application.dataPath + "/Scripts/Game/Window/";
        window.InitExitsFileInfo();
    }
    public WindowInfo info;

    Dictionary<WindowData, bool> generateCodeWindows = new Dictionary<WindowData, bool>();

    List<string> exitsWindows = new List<string>();

    public static string WindowPath;

    public static string CodeOutPath ;

    public string relativePath;

    public Vector2 scrollPos = new Vector2();

    private void OnGUI()
    {
        GUILayout.BeginVertical();
        GUILayout.BeginHorizontal();
        GUILayout.Label("文件路径 ：" + relativePath);
        if (GUILayout.Button("读取"))
        {
            string dir = string.IsNullOrEmpty(WindowPath) ? Application.dataPath : WindowPath;
            Debug.Log(Path.GetDirectoryName(dir));
            WindowPath = EditorUtility.OpenFilePanel("选中Window文件",Path.GetDirectoryName(dir) , "");
            PlayerPrefs.SetString("WindowPath", WindowPath);
            relativePath = PathUtils.GetRelativePath("Assets", WindowPath);
            InitExitsFileInfo();
        }

        GUILayout.EndHorizontal();

        GUILayout.BeginVertical();
        scrollPos = GUILayout.BeginScrollView(scrollPos,false,true);
        if (info!=null)
        {
            for (int i = 0; i < info.Data.Count; i++)
            {
                bool isExits = exitsWindows.Contains(info.Data[i].PrefabName);
                GUILayout.BeginHorizontal();
                GUILayout.Label("名称 ：" + info.Data[i].Name+"   ",GUILayout.Width(150));
                GUILayout.Label("是否已经存在 ：" + isExits + "   ");
                if (!isExits)
                {
                    generateCodeWindows[info.Data[i]] = GUILayout.Toggle(generateCodeWindows[info.Data[i]], "");
                }
                GUILayout.EndHorizontal();
            }
        }

        GUILayout.BeginHorizontal();
        if (GUILayout.Button("自动生成初步窗体代码"))
        {
            foreach (var item in generateCodeWindows)
            {
                if (item.Value)
                {
                    string path = CodeOutPath + item.Key.PrefabName + ".cs";
                   FileStream fs= File.Create(path);
                    fs.Close();
                    File.WriteAllText(path, UICodeUtils.GenerateCode(item.Key));
                }
            }
            AssetDatabase.Refresh();
            InitExitsFileInfo();
        }

        GUILayout.EndHorizontal();

        GUILayout.EndVertical();

        GUILayout.EndScrollView();

        GUILayout.EndVertical();
    }

    private void OnProjectChange()
    {
        InitExitsFileInfo();
    }

    public void InitExitsFileInfo()
    {
        if (!string.IsNullOrEmpty(relativePath))
        {
            info = AssetDatabase.LoadAssetAtPath<WindowInfo>(relativePath);
            generateCodeWindows.Clear();
            for (int i = 0; i < info.Data.Count; i++)
            {
                generateCodeWindows.Add(info.Data[i], false);
            }
        }
        DirectoryInfo dirInfo = new DirectoryInfo(CodeOutPath);
        FileInfo[] files = dirInfo.GetFiles();
        List<string> paths = ResFileUtils.SelectFilesByFileFilter(files, new string[] { ExtensionFilter.Meta });
        exitsWindows.Clear();
        for (int i = 0; i < paths.Count; i++)
        {
            exitsWindows.Add(PathUtils.GetFileNameWithoutExtension(paths[i]));
        }
    }
}